<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Job extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(null, true);
		$this->lang->load('candidate', $this->session->userdata['user_lang']);
		$this->lang->load('company', $this->session->userdata['user_lang']);
		$this->lang->load('job', $this->session->userdata['user_lang']);
	}
	
	public function detail($job_id = null, $job_text = '')
	{
		$data['main_content'] = 'job/detail';
		
		$this->load->model('Job_model', 'job');
		$this->load->model('Company_model', 'company');
		if (is_numeric($job_id))
		{
			$data['job'] = $this->job->get(array('idjob' => $job_id))->row();
			$data['company'] = $this->company->get(array('idcompany' => $data['job']->idcompany));
			
			$keywords = array();
			$keywords_list = $this->job->get_job_keywords(array('idjob' => $job_id));
			foreach ($keywords_list->result() as $line)
			{
				$keywords[] = $line->name;
			}
			$data['job']->keywords = implode(', ', $keywords);
		}
		
		$data['page_title'] = $data['job']->title;
		$data['userdata'] = $this->session->userdata;
		
		switch ($this->session->userdata['idtype']) {
			case 2:
				$this->load->view('template-company', $data);
				break;
			
			default:
				$this->load->view('template-candidate', $data);
				break;
		}
	}
	
	public function request($request_type = null, $job_id = null, $job_title = '')
	{
		switch ($request_type)
		{
			case 'interview':	
				
				$this->check_authentication(1);
				
				$this->load->model('Job_model', 'job');
				$job_data = $this->job->get(array('idjob' => $job_id))->row();
				
				$this->load->model('Subscription_model', 'interview');
				
				$data = array();
				$data['idjob'] = $job_id;
				$data['idcandidate'] = $this->session->userdata['idprofile'];
				
				if ($this->interview->get($data)->row())
				{
					exitHTML('showBox("'.'Você já está inscrito a esta vaga.'.'");', true);
				}
				
				$data['idstatus'] = 1;
				
				if ($this->interview->insert($data) === false)
				{
				   exitHTML('showBox("'.$this->lang->line('global_systemfail').'");', true);
				}
				else
				{
					exitHTML('showBox("'.'Você se candidatou com sucesso para a vaga:<br/><br/><b>'.$job_data->title.'</b>");', true);
				}
				break;
		}
	}
}

?>